package com.csi.filter;

import com.alibaba.fastjson2.JSON;
import com.csi.domain.User;
import com.csi.pojo.Result;
import com.csi.pojo.TokenState;
import com.csi.utils.JWTUtils;


import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter("/*")
public class JwtAuthenticationFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;

        String token = request.getParameter("token");
        String longToken = request.getParameter("longToken");

        Result<User> result = new Result<>();
        String servletPath = request.getServletPath();

        // 请求图片放行
        if (servletPath.startsWith("/uploads/")) {
            filterChain.doFilter(request, response);
            return;
        }

        // 请求二维码图片放行
        if (servletPath.startsWith("/QR/")) {
            filterChain.doFilter(request, response);
            return;
        }

        // 登录放行
        if ("/user/loginController".equals(servletPath)) {
            filterChain.doFilter(request, response);
            return;
        }

        // 手机号登录放行
        if ("/user/sendMessageAfterController".equals(servletPath)) {
            filterChain.doFilter(request, response);
            return;
        }

        // 手机号验证码放行
        if ("/user/sendMessageController".equals(servletPath)) {
            filterChain.doFilter(request, response);
            return;
        }

        // 注册放行
        if ("/user/registerController".equals(servletPath)) {
            filterChain.doFilter(request, response);
            return;
        }

        // 忘记密码放行
        if ("/user/forgotPasswordController".equals(servletPath)) {
            filterChain.doFilter(request, response);
            return;
        }

        if (token != null && longToken != null){
            TokenState tokenState = JWTUtils.parseJWT(token);

            switch (tokenState.getNum()) {
                case -1:
                    tokenState = JWTUtils.parseJWT(longToken);

                    if (tokenState.getNum() != -1) {

                        token = JWTUtils.createJWT(1000 * 60 * 60 * 3, tokenState.getUserId(), tokenState.getRoles(),tokenState.getUserNumber());

                        longToken = JWTUtils.createJWT(1000 * 60 * 60 * 24, tokenState.getUserId(), tokenState.getRoles(),tokenState.getUserNumber());

                        User user = new User();

                        user.setUserId(tokenState.getUserId());
                        user.setRoles(tokenState.getRoles());
                        user.setToken(token);
                        user.setLongToken(longToken);

                        result.setCode(2000);
                        result.setMsg("续期成功!");
                        result.setDatas(user);

                        filterChain.doFilter(request, response);
                        return;
                    } else {
                        result.setCode(4000);
                        result.setMsg("续期失败,登录状态已过期!");
                    }
                    break;
                case -2:
                    result.setCode(5000);
                    result.setMsg("非法操作登录系统!");
                    break;
                default:
                    filterChain.doFilter(request, response);
                    return;
            }
        }else {
            result.setCode(6000);
            result.setMsg("请先登录!");
        }

        response.setContentType("application/json;charset=utf-8");
        String json = JSON.toJSONString(result);
        response.getWriter().write(json);
    }

    @Override
    public void destroy() {
    }
}
